#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include "idc_ubus_lte.h"
#include "lte_service.h"
bool isEmpty(const char *str) {
        if (str != NULL) {
                if (strlen(str) == 0) {
                        return true;
                }
                return false;
        }
        return true;
}

int lte1_init(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        struct blob_attr *tb[3];
        char at_command[50]={0};
        int resStatus = 0;
        char errCode = 0;
        char response[128];
        blobmsg_parse(lte_init_policy, 3, tb, blob_data(msg), blob_len(msg));
        blob_buf_init(&b, 0);
        if (serial_init("/dev/ttyUSB2") != 0) { // 根据实际设备节点修改
                return -1;
        }
        // 1. 基础AT测试
        if (send_at_command(serial_fd, "AT", NULL, NULL, 0) != 0) {
                resStatus = -1;
                errCode |= (1 << 0);
        }
        if (send_at_command(serial_fd, "ATE0", NULL, response, sizeof(response)) != 0) {
                printf("ATE0: %s\n", response);
        }
        if(tb[1]){
                memset(at_command, 0, 50);
                sprintf(at_command, "AT+QCFG=\"nat\",%d", blobmsg_get_u32(tb[1]));
                printf("exe ... %s\n", at_command);
                if (send_at_command(serial_fd, at_command, NULL, NULL, 0) != 0) {
                        
                        resStatus = -1;
                        errCode |= (1 << 1);
                }
        }

        if(tb[2]){
                memset(at_command, 0, 50);
                sprintf(at_command, "AT+QCFG=\"usbnet\",%d", blobmsg_get_u32(tb[2]));
                printf("exe ... %s\n", at_command);
                if (send_at_command(serial_fd, at_command, NULL, NULL, 0) != 0) {
                        resStatus = -1;
                        errCode |= (1 << 2);
                }
        }
        
        if(tb[0]){
                memset(at_command, 0, 50);
                sprintf(at_command, "AT+QICSGP=1,3,\"%s\",\"\",\"\",0", blobmsg_get_string(tb[0]));
                printf("exe ... %s\n", at_command);
                if (send_at_command(serial_fd, at_command, NULL, NULL, 0) != 0) {
                        resStatus = -1;
                        errCode |= (1 << 3);
                }
        }
        if (send_at_command(serial_fd, "AT+QNETDEVCTL=1,3,1", NULL, NULL, 0) != 0) {
                resStatus = -1;
                errCode |= (1 << 4);
        }
        
        blobmsg_add_u32(&b, "status", resStatus);
        blobmsg_add_u32(&b, "errCode", errCode);
        close(serial_fd);
        ubus_send_reply(ctx, req, b.head);
        return 0;
}
int get_lte1_init(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        struct blob_attr *tb[3];
        char at_command[50]={0};
        int resStatus = 0;
        char errCode = 0;
        char response[128];
        blobmsg_parse(lte_init_policy, 3, tb, blob_data(msg), blob_len(msg));
        blob_buf_init(&b, 0);
        if (serial_init("/dev/ttyUSB2") != 0) { // 根据实际设备节点修改
                return -1;
        }
        // 1. 基础AT测试
        if (send_at_command(serial_fd, "AT", NULL, NULL, 0) != 0) {
                resStatus = -1;
                errCode |= (1 << 0);
        }
        if (send_at_command(serial_fd, "ATE0", NULL, response, sizeof(response)) != 0) {
                printf("ATE0: %s\n", response);
        }
        if(tb[1]){
                if (send_at_command(serial_fd, "AT+QCFG=\"nat\"", "+QCFG:", NULL, 0) != 0) {
                        
                        resStatus = -1;
                        errCode |= (1 << 1);
                }
        }

        if(tb[2]){
                if (send_at_command(serial_fd, "AT+QCFG=\"usbnet\"", "+QCFG:", NULL, 0) != 0) {
                        resStatus = -1;
                        errCode |= (1 << 2);
                }
        }
        
        if(tb[0]){
                memset(at_command, 0, 50);
                sprintf(at_command, "AT+QICSGP=1,3,\"%s\",\"\",\"\",0", blobmsg_get_string(tb[0]));
                printf("exe ... %s\n", at_command);
                if (send_at_command(serial_fd, at_command, NULL, NULL, 0) != 0) {
                        resStatus = -1;
                        errCode |= (1 << 3);
                }
        }
        if (send_at_command(serial_fd, "AT+QNETDEVCTL=1,3,1", NULL, NULL, 0) != 0) {
                resStatus = -1;
                errCode |= (1 << 4);
        }
        blobmsg_add_u32(&b, "status", resStatus);
        blobmsg_add_u32(&b, "errCode", errCode);
        close(serial_fd);
        ubus_send_reply(ctx, req, b.head);
        return 0;
}
int lte1_info(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        
        blob_buf_init(&b, 0);
        LteInfo lteInfo;
        if (serial_init("/dev/ttyUSB2") != 0) { // 根据实际设备节点修改
                return -1;
        }
        // 1. 基础AT测试
        if (send_at_command(serial_fd, "AT", NULL, NULL, 0) != 0) {
                printf("Module not responding!\n");
                return -1;
        }
        printf("Module is alive.\n");
        char response[256];
        // 2. 查询SIM卡状态
        if (send_at_command(serial_fd, "AT+CPIN?", "+CPIN:", response, sizeof(response)) == 0) {
                printf("SIM Status: %s\n", response);
        }
        if (send_at_command(serial_fd, "ATE0", NULL, response, sizeof(response)) == 0) {
                printf("ATE0: %s\n", response);
        }
        // 3. 查询网络注册状态
        if (send_at_command(serial_fd, "AT+CGMI", NULL, response, sizeof(response)) == 0) {
                sscanf(response, "%s", &lteInfo.cgmi);
        }
        if (send_at_command(serial_fd, "AT+CGMM", NULL, response, sizeof(response)) == 0) {
                sscanf(response, "%s", &lteInfo.cgmm);
        }
        if (send_at_command(serial_fd, "AT+GSN", NULL, response, sizeof(response)) == 0) {
                sscanf(response, "%s", &lteInfo.gsn);
        }
        if (send_at_command(serial_fd, "AT+QCCID", "+QCCID:", response, sizeof(response)) == 0) {
                sscanf(response, "%s", &lteInfo.qccid);
        }
        if (send_at_command(serial_fd, "AT+CSQ", "+CSQ:", response, sizeof(response)) == 0) {
                sscanf(response, "%d,%d", &lteInfo.signalPower, &lteInfo.signalQuality);
        }
        if (send_at_command(serial_fd, "AT+QGDCNT", "+QGDCNT:", response, sizeof(response)) == 0) {
                sscanf(response, "%ld,%ld", &lteInfo.bytes_sent,&lteInfo.bytes_recv);
        }
        
        if (send_at_command(serial_fd, "AT+CGPADDR=1", "+CGPADDR:", response, sizeof(response)) == 0) {
                sscanf(response, " 1,\"%[^\"]\"", &lteInfo.cgpaddr);
        }
        if (send_at_command(serial_fd, "AT+CREG?", "+CREG:", response, sizeof(response)) == 0) {
                int n;
            sscanf(response, " %d,%d", &n, &lteInfo.creg);
        }
        ServingCellInfo cell_info;
        if (send_at_command(serial_fd, "AT+QENG=\"servingcell\"", "+QENG:", response, sizeof(response)) == 0) {
                printf("Network Registration: %s\n", response);
                char *res = strstr(response, "CONNECT");
                if(res != NULL) {  //拨号成功
                        //"servingcell","CONNECT","LTE","FDD",460,00,553747,425,1300,3,5,5,8309,-90,-12,-58,-3,4,0,38
                        // 调用解析函数
                        if (parseQENG(response, &cell_info) == 0) {
                        }
                } else {        //拨号失败
                }
        }
        if (send_at_command(serial_fd, "AT+QLTS=2", "+QLTS:", response, sizeof(response)) != 0) {
                printf("Network Time: %s\n", response);
        }
        close(serial_fd);

        if(!isEmpty(lteInfo.cgmi)) blobmsg_add_string(&b, "cgmi", lteInfo.cgmi);
        if(!isEmpty(lteInfo.cgmm)) blobmsg_add_string(&b, "cgmm", lteInfo.cgmm);
        if(!isEmpty(lteInfo.gsn)) blobmsg_add_string(&b, "gsn", lteInfo.gsn);
        if(!isEmpty(lteInfo.qccid)) blobmsg_add_string(&b, "qccid", lteInfo.qccid);
        blobmsg_add_u32(&b, "signalPower", lteInfo.signalPower);
        blobmsg_add_u32(&b, "signalQuality", lteInfo.signalQuality);
        blobmsg_add_u32(&b, "bytes_sent", lteInfo.bytes_sent);
        blobmsg_add_u32(&b, "bytes_recv", lteInfo.bytes_recv);

        if(!isEmpty(lteInfo.cgpaddr)) blobmsg_add_string(&b, "cgpaddr", lteInfo.cgpaddr);
        blobmsg_add_u32(&b, "creg", lteInfo.creg);

        if(!isEmpty(cell_info.state)) blobmsg_add_string(&b, "state", cell_info.state);
        if(!isEmpty(cell_info.rat)) blobmsg_add_string(&b, "RAT", cell_info.rat);
        if(!isEmpty(cell_info.duplex_mode)) blobmsg_add_string(&b, "is_tdd", cell_info.duplex_mode);
        
        blobmsg_add_u32(&b, "MCC", cell_info.mcc);
        blobmsg_add_u32(&b, "MNC", cell_info.mnc);
        blobmsg_add_u32(&b, "cellID", cell_info.cell_id);
        blobmsg_add_u32(&b, "EARFCN", cell_info.earfcn);
        // blobmsg_add_u32(&b, "freq_band_ind", cell_info.freq_band_ind);
        blobmsg_add_u32(&b, "UL_bandwidth", cell_info.ul_bandwidth);
        blobmsg_add_u32(&b, "DL_bandwidth", cell_info.dl_bandwidth);
        blobmsg_add_u32(&b, "TAC", cell_info.tac);
        blobmsg_add_u32(&b, "PCI", cell_info.pci);
        blobmsg_add_u32(&b, "RSRP", cell_info.rsrp);
        blobmsg_add_u32(&b, "RSRQ", cell_info.rsrq);
        blobmsg_add_u32(&b, "RSSI", cell_info.cqi);
        blobmsg_add_u32(&b, "SINR", cell_info.sinr);
        blobmsg_add_u32(&b, "srxlev", cell_info.srxlev);
        blobmsg_add_u32(&b, "tx_porwr", cell_info.tx_power);
        ubus_send_reply(ctx, req, b.head);
        return 0;
} 

int lte2_base(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        blob_buf_init(&b, 0);
        #if 0
        LteInfo *info = read_lte2_base();
        if(info != NULL){
                if(!isEmpty(info->cgmi)) blobmsg_add_string(&b, "cgmi", info->cgmi);
                if(!isEmpty(info->cgmm)) blobmsg_add_string(&b, "cgmm", info->cgmm);
                if(!isEmpty(info->gsn)) blobmsg_add_string(&b, "gsn", info->gsn);
                if(!isEmpty(info->qccid)) blobmsg_add_string(&b, "qccid", info->qccid);
        }
        #endif
        ubus_send_reply(ctx, req, b.head);
        return 0;
} 
int lte2_info(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        blob_buf_init(&b, 0);
        LteInfo info;
        #if 0
        int res = read_lte2_info(&info);
        if(res == 0) {
                blobmsg_add_u32(&b, "signalPower", info.signalPower);
                blobmsg_add_u32(&b, "signalQuality", info.signalQuality);
                if(!isEmpty(info.state)) blobmsg_add_string(&b, "state", info.state);
                if(!isEmpty(info.RAT)) blobmsg_add_string(&b, "RAT", info.RAT);
                if(!isEmpty(info.is_tdd)) blobmsg_add_string(&b, "is_tdd", info.is_tdd);
                if(!isEmpty(info.cgpaddr)) blobmsg_add_string(&b, "cgpaddr", info.cgpaddr);
                blobmsg_add_u32(&b, "creg", info.creg);
                blobmsg_add_u32(&b, "MCC", info.MCC);
                blobmsg_add_u32(&b, "MNC", info.MNC);
                blobmsg_add_u32(&b, "cellID", info.cellID);
                blobmsg_add_u32(&b, "EARFCN", info.EARFCN);
                blobmsg_add_u32(&b, "freq_band_ind", info.freq_band_ind);
                blobmsg_add_u32(&b, "UL_bandwidth", info.UL_bandwidth);
                blobmsg_add_u32(&b, "DL_bandwidth", info.DL_bandwidth);
                blobmsg_add_u32(&b, "TAC", info.TAC);
                blobmsg_add_u32(&b, "PCI", info.PCI);
                blobmsg_add_u32(&b, "RSRP", info.RSRP);
                blobmsg_add_u32(&b, "RSRQ", info.RSRQ);
                blobmsg_add_u32(&b, "RSSI", info.RSSI);
                blobmsg_add_u32(&b, "SINR", info.SINR);
                blobmsg_add_u32(&b, "srxlev", info.srxlev);
                blobmsg_add_u32(&b, "bytes_sent", info.bytes_sent);
                blobmsg_add_u32(&b, "bytes_recv", info.bytes_recv);
        }
        #endif
        ubus_send_reply(ctx, req, b.head);
        return 0;
} 
